package edu.hawaii.halealohacli.processor;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.List;
import org.junit.Test;
import edu.hawaii.halealohacli.command.Command;
import edu.hawaii.halealohacli.command.FakeCommand;

/**
 * Tests that Command classes can be found if they are in the 
 * 'edu.hawaii.halealohacli.command' package.
 * @author Leo deCandia
 */
@SuppressWarnings("rawtypes")
public class TestCommandFinderUtils {

  /**
   * Test fake command is found.
   */
  @Test
  public void testFakeCommandIsFound() {
    boolean foundFakeCommand = false;
    try {
      List<Class> classesInPackage = 
          CommandFinderUtils.getClasses("edu.hawaii.halealohacli.command");
        for (Class klass : classesInPackage) {
          if (!klass.isInterface() && FakeCommand.class.isAssignableFrom(klass)) {
              Command command;
              try {
                command = (Command) klass.newInstance();
                String commandSlug = command.getCommandName();
                assertEquals("wrong command name", "fake-command", commandSlug);
                foundFakeCommand = true;
              }
              catch (InstantiationException e) {
                fail(e.toString());
              }
              catch (IllegalAccessException e) {
                fail(e.toString());
              }
          }
        }
        assertTrue("couldn't find FakeCommand class", foundFakeCommand);
    }
    catch (ClassNotFoundException e) {
      fail(e.toString());
    }
    catch (IOException e) {
      fail(e.toString());
    }
  }

}
